package spikes

import (
	"context"
	"enjoy-shop/formdata"
	"fmt"
	goodspb "gitee.com/enjoy_5/goods-rpc/client"
	"gitee.com/enjoy_5/spikes-rpc/client/spikes"
	pb "gitee.com/enjoy_5/spikes-rpc/proto/spikes"
	"github.com/shopspring/decimal"
	"strconv"
	"time"
)

// 创建秒杀活动
func CreateSpikesActivity(data formdata.CreateSpikesActivity) (*pb.CreateSpikesActivityResponse, error) {
	s := &pb.CreateSpikesActivityRequest{
		UserID:         data.UserID,
		SpikesTitle:    data.SpikesTitle,
		SpikeStartTime: data.SpikeStartTime,
		SpikeEndTime:   data.SpikeEndTime,
	}
	return spikes.CreateSpikesActivity(s)
}

// 获取秒杀商品详情
func GetGoodsInfo(data formdata.GetSpikesGoodsInfo) ([]formdata.SpikesGoodsDetail, error) {
	goodsID, _ := strconv.Atoi(data.GoodsID)
	activityID, _ := strconv.Atoi(data.ActivityID)
	skuID, _ := strconv.Atoi(data.SkuID)
	//查询商品信息
	info, err := goodspb.GetGoodsInfo(context.Background(), int64(goodsID))
	//skuId 查询秒杀商品价格
	detail, err := spikes.GetSpikesGoodsDetail(&pb.GetSpikesGoodsDetailRequest{
		SkuID:            int64(skuID),
		SpikesActivityID: int64(activityID),
	})
	if err != nil {
		return nil, err
	}
	//单个 skuid 查询
	ds, err := goodspb.GetSkuInfoByIDs(context.Background(), detail.In.SkuID)
	if err != nil {
		return nil, err
	}
	var spikesGoodsInfo []formdata.SpikesGoodsDetail
	spikesGoodsInfo = append(spikesGoodsInfo, formdata.SpikesGoodsDetail{
		GoodsID:       info.Id,
		Title:         info.Title,
		Point:         info.Point,
		Description:   info.Description,
		Img:           info.Img,
		PropertyInfos: info.PropertyInfos,
		SpikesID:      detail.In.SpikesID,
		SkuID:         detail.In.SkuID,
		Price:         ds.Price,
		SpikesStock:   detail.In.SpikesStock,
		SpikesPrice:   detail.In.SpikesPrice,
	})
	return spikesGoodsInfo, nil
}

// 获取秒杀商品列表
func GetSpikesGoodsList(data formdata.GetSpikesGoodsList) ([]formdata.SpikesGoodsInfo, error) {
	s := &pb.GetSpikesGoodsListRequest{
		SpikesActivityID: data.SpikesActivityID,
		Page:             data.Page,
		PageSize:         data.PageSize,
	}
	// 查询到参与活动的商品skuId (切片)
	goodsSkuIdList, err := spikes.GetSpikesGoodsList(s)
	if err != nil {
		return nil, err
	}
	ss := make(map[int64]formdata.SpikesGoodsInfo)
	// 储存sku id
	var skuIds []int64
	// 储存sku id，并初始化秒杀商品信息
	for _, val := range goodsSkuIdList.GetSpikesList() {
		ss[val.GoodsSkuID] = formdata.SpikesGoodsInfo{
			SkuID:       val.GoodsSkuID,
			SpikesStock: val.SpikesStock,
			SpikesPrice: val.SpikesPrice,
		}
		skuIds = append(skuIds, val.GoodsSkuID)
	}
	// 调用切片商品Sku查询
	goodsSkuInfo, err := goodspb.GetGoodsInfoBySkuIds(context.Background(), skuIds)
	if err != nil {
		return nil, err
	}
	for _, val := range goodsSkuInfo.GetInfo() {
		goodsPrice := decimal.NewFromFloat32(val.Price).StringFixed(2)
		ss[val.GoodsId] = formdata.SpikesGoodsInfo{
			GoodsID:     val.GoodsId,
			GoodsImg:    val.Img,
			GoodsTitle:  val.Title,
			GoodsPrice:  goodsPrice,
			SkuID:       ss[val.GoodsId].SkuID,
			SpikesStock: ss[val.GoodsId].SpikesStock,
			SpikesPrice: ss[val.GoodsId].SpikesPrice, // 使用秒杀价格
		}
	}
	var info []formdata.SpikesGoodsInfo
	for _, val := range ss {
		info = append(info, formdata.SpikesGoodsInfo{
			GoodsID:     val.GoodsID,
			GoodsImg:    val.GoodsImg,
			GoodsTitle:  val.GoodsTitle,
			SpikesStock: val.SpikesStock,
			SkuID:       val.SkuID,
			GoodsPrice:  val.GoodsPrice,
			SpikesPrice: val.SpikesPrice,
		})
	}
	return info, nil
}

// 修改秒杀库存
func UpdateSpikesStock(data formdata.UpdateSpikesStock) (*pb.UpdateSpikesStockByGoodsIDResponse, error) {
	s := &pb.UpdateSpikesStockByGoodsIDRequest{
		GoodsID:          data.GoodsID,
		SpikesActivityID: data.SpikesActivityID,
		Num:              data.Num,
	}

	return spikes.UpdateSpikesStock(s)
}

// 获取秒杀活动列表
func GetSpikesActivityList() (*pb.GetSpikesActivityListResponse, error) {
	s := &pb.GetSpikesActivityListRequest{}
	list, err := spikes.GetSpikesActivityList(s)
	if err != nil {
		return nil, err
	}
	for _, val := range list.SpikesActivityList {
		timeStartTime := val.SpikeStartTime
		parse, err := time.Parse(time.RFC3339, timeStartTime)
		if err != nil {
			return nil, err
		}
		//格式化时间
		times := parse.Format("15:04")
		fmt.Println("时间：：", times)
		val.SpikeStartTime = times
	}
	return list, nil
}

// 创建秒杀商品sku关联活动方法
func CreateSpikesGoodsSku(data formdata.CreateSpikesGoodsSku) (*pb.CreateSpikesGoodsSkuResponse, error) {
	s := &pb.CreateSpikesGoodsSkuRequest{
		SpikesActivityID: data.SpikesID,
		GoodsSkuID:       data.SkuID,
		SpikesStock:      data.SpikesStock,
		Price:            data.Price,
		SpikesPrice:      data.SpikesPrice,
		UserID:           data.UserID,
	}
	return spikes.CreateSpikesGoodsSku(s)
}
